var curFiles = [];
var downUrl = [];
var title;

function chexiao(id) {
    vm.tianan.id = id;
    vm.tianan.delFlag = "1";
    var url = vm.tianan.id == null ? "sys/tianan/save" : "sys/tianan/update";
    $.ajax({
        type: "POST",
        url: baseURL + url,
        contentType: "application/json",
        data: JSON.stringify(vm.tianan),
        success: function (r) {
            if (r.code === 0) {
                layer.msg("撤销成功", {icon: 1});
                vm.reload();
            } else {
                layer.alert(r.msg);

            }
        }
    });

}

$(function () {
    vm.initselect();
    $.get(baseURL + "sys/tianan/start", function (r) {
        $(".panel-heading").html("提案提交截止时间：" + r.endtime);
        if (r.start == "1")
            $(".form-horizontal").html("<h3 style='text-align: center;'>提案提交已截止。</h3>")
    });

});

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: false,
            title: null,
            dictlist: getDict('tiantype'),
            userlist: getuserlisttype(0),
            deptlist: getDeptAlls(''),
            user: getuser(),
            tianan: {
                id: '',
                type: '1',
                types: '',
                title: '',
                fuyiren: '',
                lianming: '',
                deptId: '',
                anyoufenxi: '',
                cuoshijianyi: '',
            },
            editor: '',
            editor1: '',
            demo1: '',
        },
        methods: {
            init: function () {

                var E = window.wangEditor;
                vm.editor = new E('#anyoufenxi');
                vm.editor1 = new E('#zhenggaicuoshi');

                vm.editor.config.menus = [];
                vm.editor1.config.menus = [];
                // 或者 const editor = new E( document.getElementById('div1') )
                vm.editor.create();
                vm.editor1.create();
                $(".qiehuan").click(function () {

                    let type = this.getAttribute('value');
                    vm.tianan.type = type;
                    vm.show(type);
                });
                if (vm.user.iszuzhang != 1) {
                    $("#type3").hide();
                }
            },
            show: function (type) {
                $(".activebut").removeClass("activebut");
                $("#type" + type + "").addClass("activebut");
                if (type === '1') {
                    $('#fyr').show();
                    $('#lmr').hide();
                    $('#bm').hide();
                }
                if (type === '2') {
                    $('#fyr').hide();
                    $('#lmr').show();
                    $('#bm').hide();
                }
                if (type === '3') {
                    $('#fyr').hide();
                    $('#lmr').hide();
                    $('#bm').show();
                }


            },
            initselect: function () {
                var userlist = getuserlisttype(0);

                vm.demo1 = xmSelect.render({
                    el: '#demo1',
                    toolbar: {
                        show: true,
                    },
                    theme: {
                        color: '#0081ff',
                    },
                    filterable: true,
                    paging: true,
                    pageSize: 100,
                    data: []
                });

                var user = [];
                for (var i = 0; i < userlist.length; i++) {
                    var data = {name: userlist[i].realname, value: userlist[i].userId};
                    user.push(data)
                }
                vm.demo1.update({
                    data: user,
                    autoRow: true,
                });
            },
            query: function () {
                vm.reload();
            },
            add: function () {
                vm.showList = false;
                vm.title = "新增";
                vm.demo1.setValue([]);
                vm.editor1.txt.html("");
                vm.editor.txt.html("");
                vm.tianan = {
                    type: '1',
                    types: '',
                    title: '',
                    fuyiren: '',
                    lianming: '',
                    deptId: '',
                    anyoufenxi: '',
                    cuoshijianyi: '',
                };
                vm.show(vm.tianan.type);
            },
            update: function (ids) {
                var id = ids != null ? ids : getSelectedRow();
                if (id == null) {
                    return;
                }
                vm.showList = false;
                vm.title = "修改";
                vm.getInfo(id)
            },
            saveOrUpdate: function (event) {
                vm.tianan.lianmingstatus = null;
                vm.tianan.fuyistatus = null;
                vm.tianan.fuyiren = vm.demo1.getValue("value");
                // vm.tianan.deptId=$("#deptId :selected").val()
                // vm.tianan.lianming=$("#lianming :selected").val()
                // vm.tianan.types=$("#types :selected").val()
                vm.tianan.cuoshijianyi = vm.editor1.txt.html();
                vm.tianan.anyoufenxi = vm.editor.txt.html();

                if (vm.tianan.type == '1') {
                    vm.tianan.lianming ="";
                    if (vm.tianan.fuyiren != '' && vm.tianan.fuyiren.length < 2) {
                        layer.alert("附议人至少两名", {icon: 2});
                        return;
                    } else {
                        if (vm.tianan.fuyiren.length == 0 || vm.tianan.fuyiren == null || vm.tianan.fuyiren == true || vm.tianan.fuyiren == '') {
                            vm.tianan.yushenstatus = 0;
                            vm.tianan.fuyiren = "";
                        } else {
                            vm.tianan.fuyiren = vm.tianan.fuyiren.join();
                            vm.tianan.fuyistatus = 0;
                            vm.tianan.yushenstatus = -1;
                        }
                    }
                }
                if (vm.tianan.type == 2) {
                    vm.tianan.fuyiren = "";
                    vm.tianan.lianmingstatus = 0;
                    if (vm.tianan.lianming.length == 0 || vm.tianan.lianming == null || vm.tianan.lianming == '') {
                        layer.alert("联名人为必选项", {icon: 2});
                        return;
                    } else {
                        vm.tianan.yushenstatus = -1;
                        vm.tianan.fuyiren = "";
                    }
                }
                if (vm.tianan.type == 3) {
                    vm.tianan.yushenstatus = 0;
                }

                if (nulls(vm.tianan.cuoshijianyi) || nulls(vm.tianan.anyoufenxi) || nulls(vm.tianan.title) || nulls(vm.tianan.title) || nulls(vm.tianan.types)) {
                    layer.alert("提交失败,数据不完整");
                    return;
                }

                $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {

                    var url = "sys/tianan/save";
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.tianan),
                        success: function (r) {
                            if (r.code === 0) {
                                layer.msg("操作成功", {icon: 1});
                                $('#btnSaveOrUpdate').button('reset');
                                $('#btnSaveOrUpdate').dequeue();
                                top.window.location.hash = "modules/sys/tianan.html";
                            } else {
                                layer.alert(r.msg);
                                $('#btnSaveOrUpdate').button('reset');
                                $('#btnSaveOrUpdate').dequeue();
                            }
                        }
                    });
                });


            },
            del: function (event) {
                var ids = getSelectedRows();
                if (ids == null) {
                    return;
                }
                var lock = false;
                layer.confirm('确定要删除选中的记录？', {
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    if (!lock) {
                        lock = true;
                        $.ajax({
                            type: "POST",
                            url: baseURL + "sys/tianan/delete",
                            contentType: "application/json",
                            data: JSON.stringify(ids),
                            success: function (r) {
                                if (r.code == 0) {
                                    layer.msg("操作成功", {icon: 1});
                                    $("#jqGrid").trigger("reloadGrid");
                                } else {
                                    layer.alert(r.msg);
                                }
                            }
                        });
                    }
                }, function () {
                });
            }
            ,
            getInfo: function (id) {
                $.get(baseURL + "sys/tianan/info/" + id, function (r) {
                    vm.tianan = r.tianan;
                    vm.demo1.setValue(vm.tianan.fuyiren.split(","));
                    vm.editor1.txt.html(vm.tianan.cuoshijianyi);
                    vm.editor.txt.html(vm.tianan.anyoufenxi);
                    vm.show(vm.tianan.type);
                });
            }
            ,
            reload: function (event) {
                vm.showList = true;
                var page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    page: page
                }).trigger("reloadGrid");
            }
        }
    })
;
vm.init();